﻿using Aspose.Cells;
using DeviceManager.DB;
using DeviceManager.DB.Model;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace DeviceManager.ContentPage.SysMng
{
    /// <summary>
    /// ExportDeviceInfo.xaml 的交互逻辑
    /// </summary>
    public partial class ExportDeviceInfo : Window
    {

        List<DeviceNode> deviceNodes = null;

        public ExportDeviceInfo()
        {
            InitializeComponent();
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try {
                this.DragMove();
            } catch { }
        }

        private void blt_selectFile_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
            ofd.DefaultExt = ".xls";
            ofd.Filter = "office 2007 excel file|*.xls";
            if (ofd.ShowDialog()==true)
            {
                tb_fileaddress.Text = ofd.FileName;
                deviceNodes = PaseraExcelFile(ofd.FileName);
                lv_devices.ItemsSource = deviceNodes;
            }
        }


        /// <summary>
        /// 解析Excel文件
        /// </summary>
        /// <param name="filePath"></param>
        private List<DeviceNode> PaseraExcelFile(string filePath)
        {
            Workbook book = new Workbook(filePath);
            if (book.Worksheets.Count == 0)
            {
                MessageBox.Show("模板文件不正确，不存在正确的工作表格");
                return new List<DeviceNode>();
            }
            Worksheet sheet = book.Worksheets[0];
            List<DeviceNode> devicenodes = new List<DeviceNode>();
            for(int i = 0; i < sheet.Cells.MaxDataRow + 1; i++)
            {
                try
                {
                    DeviceNode deviceNode = new DeviceNode();
                    deviceNode.DeviceCode = sheet.Cells[i, 0].StringValue;
                    deviceNode.DeviceName = sheet.Cells[i, 1].StringValue;
                    deviceNode.DeviceDes = sheet.Cells[i, 2].StringValue;
                    deviceNode.DeviceDepName = sheet.Cells[i, 3].StringValue;
                    deviceNode.DeviceTypeName = sheet.Cells[i, 4].StringValue;
                    deviceNode.DeviceInstall = sheet.Cells[i, 5].StringValue;
                    deviceNode.DeviceMaxVoltage = sheet.Cells[i, 6].FloatValue;
                    deviceNode.DeviceMinVoltage = sheet.Cells[i, 7].FloatValue;
                    deviceNode.DeviceMaxElectricCurrent = sheet.Cells[i, 8].FloatValue;
                    deviceNode.DeviceMinElectricCurrent = sheet.Cells[i, 9].FloatValue;
                    deviceNode.DeviceMaxTemperature = sheet.Cells[i, 10].FloatValue;
                    deviceNode.DeviceMinTemperature = sheet.Cells[i, 11].FloatValue;
                    deviceNode.DeviceMaxHumidity = sheet.Cells[i, 12].FloatValue;
                    deviceNode.DeviceMinHumidity = sheet.Cells[i, 13].FloatValue;
                    deviceNode.DeviceUpDateTime = sheet.Cells[i, 14].FloatValue;
                    deviceNode.installDate = sheet.Cells[i, 15].DateTimeValue;
                    devicenodes.Add(deviceNode);
                }
                catch(Exception ex)
                {
                    Debug.WriteLine(ex.ToString());
                }
            }
            return devicenodes;
        }

        private void blt_saveData_Click(object sender, RoutedEventArgs e)
        {
            if (deviceNodes != null)
            {
                DeviceNode[] tempNodes = new DeviceNode[deviceNodes.Count];
                deviceNodes.CopyTo(tempNodes);
                List<DeviceNode> nodes = new List<DeviceNode>(tempNodes);
                foreach(var de in deviceNodes)
                {
                    bool isOk = false;
                    int depID = ManagerDBCtrl.GetMng().GetDepIDByName(de.DeviceDepName);
                    if (depID > 0)
                    {
                        de.DeviceDepID = depID;
                        int typeID = ManagerDBCtrl.GetMng().GetDeviceTypeIDByName(de.DeviceTypeName);
                        if (typeID > 0)
                        {
                            de.DeviceType = typeID;
                            if (ManagerDBCtrl.GetMng().SaveDeviceNode(de))
                            {
                                isOk = true;
                            }
                        }
                    }
                    if (isOk)
                    {

                        nodes.Remove(de);
                        lv_devices.ItemsSource = nodes;
                    }
                }
                if (lv_devices.Items.Count > 0)
                {
                    MessageBox.Show("导入文件出错，当前显示的设备未成功导入,请检查数据");
                }
                else
                {
                    this.DialogResult = true;
                }
            }
        }

        private void blt_cannel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}
